package models;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.trackvoodoo.models.TrackDaysDocument.TrackDays;
import com.trackvoodoo.models.TrackDaysDocument.TrackDays.TrackElements;

import database.connector.DatabaseFactory;
import database.live.DOAStatements;
import database.live.MySqlDAO;
import engine.EngineUtils;

public class TrackDaysModel {

	private MySqlDAO d = null;
	private TrackDays tdays= null;

	
	public TrackDaysModel()
	{
		d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			try{
				tdays = TrackDays.Factory.newInstance();
				ResultSet rs = d.selectQuery(DOAStatements.SELECT_TRACKDATES); //idtrackDays, trackRef, date, description
				while (rs.next())
				{
					TrackElements a = tdays.addNewTrackElements();
					a.setTrackRef(rs.getInt(2));
					a.setTrackDate(EngineUtils.dateToCalendar(rs.getDate(3)));
					a.setDescription(rs.getString(4));
				}

				if (tdays.getTrackElementsArray().length == 0)
				{
					tdays = null;
				}
				
			}catch (SQLException e)
			{
				e.printStackTrace();
			}finally
			{
				d.closeConnection();
			}
		}
	}
	
	public TrackDays getTrackDays()
	{
		return tdays;
	}
	
}
